
// $_FILEHEADER_BEGIN ***************************
// 版权声明:Timer
// Copyright © 2012 - Timer All Rights Reserved
// 文件名称: NSString+StringFrame.m
// 创建日期: 15/8/10
// 创 建 人: 甄鑫
// 文件说明: 计算文本大小
// $_FILEHEADER_END ******************************


#import "NSString+StringFrame.h"

@implementation NSString(NSString_StringFrame)

- (CGSize)boundintRectWithLabel:(UILabel *)label
{
    NSDictionary *attribute = @{NSFontAttributeName: label.font};
    
    CGSize retSize = CGSizeZero;

    if(IOS7_OR_LATER)
    {
        retSize = [self boundingRectWithSize:label.frame.size
                           options:NSStringDrawingTruncatesLastVisibleLine |
                                   NSStringDrawingUsesLineFragmentOrigin |
                                   NSStringDrawingUsesFontLeading
                        attributes:attribute
                           context:nil].size;
    }
    else
    {
        retSize = [self sizeWithFont:label.font constrainedToSize:label.frame.size];
    }
    return retSize;
}


- (CGSize)boundingRectWithSize:(CGSize)size font:(UIFont *)font
{
    NSDictionary *attribute = @{NSFontAttributeName: font};
    CGSize retSize = CGSizeZero;
    if(IOS7_OR_LATER)
    {
        retSize = [self boundingRectWithSize:size
                                     options:NSStringDrawingTruncatesLastVisibleLine |
                                             NSStringDrawingUsesLineFragmentOrigin |
                                             NSStringDrawingUsesFontLeading
                                  attributes:attribute
                                     context:nil].size;
    }
    else
    {
        retSize = [self sizeWithFont:font constrainedToSize:size];
    }

    return retSize;
}

- (CGSize)getContentSize:(UILabel *)_label andSize:(CGSize)_size
{
    UIFont *font = _label.font;
    CGSize size  = _size;
    if (IOS7_OR_LATER){
        NSDictionary * tdic  = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
        size = [_label.text boundingRectWithSize:_size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
        size.height += 2;
        size.width = size.width >= _size.width ? _size.width : size.width;
    }else{
        size                   = [_label.text sizeWithFont:font constrainedToSize:_size lineBreakMode:NSLineBreakByWordWrapping];//ios7以上已经摒弃的这个方法
    }
    return size;
}

- (NSString *)moneySegmentation{
    if (self.length<3) {
        return @"";
    }
    NSArray *comArr = [self componentsSeparatedByString:@"."];
    NSString *tempStr = [comArr firstObject];
    NSString *finalStr = @"";
    for (long i=tempStr.length; i>0; i-=3) {
        NSString *subStr = @"";
        if (i>=3) {
           subStr = [tempStr substringWithRange:NSMakeRange(i-3, 3)];
        }else{
            subStr = [tempStr substringWithRange:NSMakeRange(0, i)];
        }
        
        finalStr = [NSString stringWithFormat:@"%@,%@",subStr,finalStr];
    }
    finalStr = [finalStr substringWithRange:NSMakeRange(0, finalStr.length-1)];
    return finalStr;
}
- (BOOL) isMobile{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|7[0-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:self]
         || [regextestcm evaluateWithObject:self]
         || [regextestct evaluateWithObject:self]
         || [regextestcu evaluateWithObject:self])) {
        return YES;
    }
    
    return NO;
}


/**
 *  验证100-100万金额
 */
- (BOOL)isMoneyWithMillion{
    NSString* emailRegex = @"^[1-9]{1}[0-9]{0,5}$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];

}


/**
 *  验证身份证号
 */
- (BOOL) validateIdentityCard
{
    BOOL flag;
    if (self.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:self];
}
@end
